<?php
session_start();        
include('system/connect.php');   

$room_id = (int)$_POST['room_id'];  
$my_id = $_SESSION['my_id'];
$message = $_POST['text'];
$message = strip_tags($message);
$message = mysql_real_escape_string($message);
$created = date('Y-m-d H:i:s',strtotime('now'));  

$sql = "SELECT
        chat2_room_users.room_id,
        chat2_room_users.user_id,
        chat2_room_users.aproved,
        chat2_room_users.id
        FROM
        chat2_room_users
        WHERE
        chat2_room_users.user_id = '$my_id' AND chat2_room_users.room_id =  '$room_id'
        ";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res)) {
   $result[] = $row;
}  
if (!empty($result[0])) {
    $sql = 'insert into `chat2_message` set `room_id`='.$room_id.', `user_id`='.$my_id.', `content`="'.$message.'", `created_at`="'.$created.'"'; 
    mysql_query($sql);  
    if ($result[0]['aproved'] == 0) {
        $sql = 'update chat2_room_users set aproved = 1 where room_id = '.$room_id.' and user_id = '.$my_id;
        mysql_query($sql);
    }
}
?>